#include "../logs/mylog.h"

using namespace mylog;
using namespace util;

void test_log()
{
    //指定日志器的输出
    // Logger::ptr logger = getLogger("Async_logger");

    // logger->debug("%s", "测试日志");
    // logger->info("%s", "测试日志");
    // logger->warn("%s", "测试日志");
    // logger->error("%s", "测试日志");
    // logger->fatal("%s", "测试日志");

    // size_t count = 0;
    // while (count < 10000)
    // {
    //     std::string tmp = "测试日志-" + std::to_string(count++);
    //     logger->fatal("%s", tmp.c_str());
    // }

    //默认日志器的标准输入输出测试
    DEBUG("%s", "测试日志");
    INFO("%s", "测试日志");
    WARN("%s", "测试日志");
    ERROR("%s", "测试日志");
    FATAL("%s", "测试日志");

    size_t count = 0;
    while (count < 10000)
    {
        std::string tmp = "测试日志-" + std::to_string(count++);
        FATAL("%s", tmp.c_str());
    }
}

int main()
{
    // 全局接口和宏函数代理测试
    std::unique_ptr<LoggerBuilder> builder(new GlobalLoggerBuilder());
    builder->buildLoggerName("Async_logger");
    builder->buildLoggerLevel(Level::value::WARN);
    builder->buildFormatter("[%c][%f:%l]%m%n");
    builder->buildLoggerType(LoggerType::LOGGER_ASYNC);
    builder->buildAsyncLoggerType(AsyncLooperType::ASYNC_UNSAFE);
    builder->buildSink<StdOutSink>();
    builder->buildSink<FileSink>("./logfile/async.log");

    builder->build();
    test_log();

    // // 全局单例日志器建造者测试
    // std::unique_ptr<LoggerBuilder> builder(new GlobalLoggerBuilder());
    // builder->buildLoggerName("Async_logger");
    // builder->buildLoggerLevel(Level::value::WARN);
    // builder->buildFormatter("[%c]%m%n");
    // builder->buildLoggerType(LoggerType::LOGGER_ASYNC);
    // builder->buildAsyncLoggerType(AsyncLooperType::ASYNC_UNSAFE);
    // builder->buildSink<StdOutSink>();
    // builder->buildSink<FileSink>("./logfile/async.log");

    // builder->build();
    // test_log();

    // // 异步日志器测试
    // std::unique_ptr<LoggerBuilder> builder(new LocalLoggerBuilder());
    // builder->buildLoggerName("Async_logger");
    // builder->buildLoggerLevel(Level::value::WARN);
    // builder->buildFormatter("[%c]%m%n");
    // builder->buildLoggerType(LoggerType::LOGGER_ASYNC);
    // builder->buildAsyncLoggerType(AsyncLooperType::ASYNC_UNSAFE);
    // builder->buildSink<StdOutSink>();
    // builder->buildSink<FileSink>("./logfile/async.log");
    // //builder->buildSink<RollBySizeSink>("./logfile/roll-", 1024 * 1024);

    // SyncLogger::ptr logger=builder->build();

    // logger->debug(__FILE__, __LINE__, "%s", "测试日志");
    // logger->info(__FILE__, __LINE__, "%s", "测试日志");
    // logger->warn(__FILE__, __LINE__, "%s", "测试日志");
    // logger->error(__FILE__, __LINE__, "%s", "测试日志");
    // logger->fatal(__FILE__, __LINE__, "%s", "测试日志");

    // size_t count = 0;
    // while(count<100000)
    // {
    //     std::string tmp="测试日志-"+std::to_string(count++);
    //     logger->fatal(__FILE__,__LINE__,"%s",tmp.c_str());
    // }

    // //缓冲区测试
    // std::ifstream ifs("./logfile/test.log",std::ios::binary);
    // if(ifs.good()==false){std::cout<<"test.log open error!\n";  return -1;}

    // ifs.seekg(0,std::ios::end);//跳转到文件尾
    // size_t fsize=ifs.tellg();//获取当前位置相对于文件起始位置的偏移量
    // ifs.seekg(0,std::ios::beg);//跳转到文件开头

    // std::string body;
    // body.resize(fsize);
    // ifs.read(&body[0],fsize);//将文件内容写入body字符串
    // if(ifs.good()==false){std::cout<<"read error!\n"; return -1;}
    // ifs.close();

    // Buffer buffer;
    // buffer.push(body.c_str(),body.size());

    // std::ofstream ofs("./logfile/tmp.log",std::ios::binary);
    // if(ofs.good()==false){std::cout<<"tmp.log open error!\n"; return -1;}
    // size_t wsize=buffer.readAbleSize();
    // //一个个写
    // for(int i=0;i<wsize;++i)
    // {
    //     ofs.write(buffer.readBegin(),1);
    //     if(ofs.good()==false){std::cout<<"write error!\n"; return -1;}
    //     buffer.moveReader(1);
    // }
    // //一次性全写完
    // //ofs.write(buffer.readBegin(),buffer.readAbleSize());
    // //if(ofs.good()==false){std::cout<<"write error!\n"; return -1;}
    // ofs.close();

    // // 同步日志器测试(建造者模式)
    // std::unique_ptr<LoggerBuilder> builder(new LocalLoggerBuilder());
    // builder->buildLoggerName("sync_logger");
    // builder->buildLoggerLevel(Level::value::WARN);
    // builder->buildFormatter("%m%n");
    // builder->buildLoggerType(LoggerType::LOGGER_SYNC);
    // builder->buildSink<FileSink>("./logfile/test.log");
    // //builder->buildSink<RollBySizeSink>("./logfile/roll-", 1024 * 1024);

    // SyncLogger::ptr logger=builder->build();

    // logger->debug(__FILE__, __LINE__, "%s", "测试日志");
    // logger->info(__FILE__, __LINE__, "%s", "测试日志");
    // logger->warn(__FILE__, __LINE__, "%s", "测试日志");
    // logger->error(__FILE__, __LINE__, "%s", "测试日志");
    // logger->fatal(__FILE__, __LINE__, "%s", "测试日志");

    // size_t cur_size = 0, count = 0;
    // while(cur_size<1024*1024*5)
    // {
    //     std::string tmp="测试日志-"+std::to_string(count++);
    //     logger->fatal(__FILE__,__LINE__,"%s",tmp.c_str());
    //     cur_size+=tmp.size();
    // }

    // // 日志器测试
    // std::string logger_name = "sync_logger";                                                           // 日志器名称
    // Level::value limit_level = Level::value::WARN;                                                     // 限制等级
    // Formatter::ptr fmt(new Formatter());                                                               // 日志格式器
    // LogSink::ptr std_sink = log::SinkFactory::create<StdOutSink>();                                    // 标准输出
    // LogSink::ptr file_sink = log::SinkFactory::create<FileSink>("./logfile/test.log");                 // 指定文件
    // LogSink::ptr roll_sink = log::SinkFactory::create<RollBySizeSink>("./logfile/roll-", 1024 * 1024); // 滚动文件
    // std::vector<LogSink::ptr> sinks = {std_sink, file_sink, roll_sink};                                // 落地方向

    // SyncLogger::ptr logger(new SyncLogger(logger_name, limit_level, fmt, sinks));
    // logger->debug(__FILE__, __LINE__, "%s", "测试日志");
    // logger->info(__FILE__, __LINE__, "%s", "测试日志");
    // logger->warn(__FILE__, __LINE__, "%s", "测试日志");
    // logger->error(__FILE__, __LINE__, "%s", "测试日志");
    // logger->fatal(__FILE__, __LINE__, "%s", "测试日志");

    // size_t cur_size = 0, count = 0;
    // while(cur_size<1024*1024*5)
    // {
    //     std::string tmp="测试日志-"+std::to_string(count++);
    //     logger->fatal(__FILE__,__LINE__,"%s",tmp.c_str());
    //     cur_size+=tmp.size();
    // }

    // // 日志落地测试
    // log::LogMsg msg(log::Level::value::INFO, 53, "main.cc", "root", "格式化功能测试...");
    // log::Formatter fmt;
    // std::string str = fmt.format(msg);

    // log::LogSink::ptr std_sink=log::SinkFactory::create<StdOutSink>();//标准输出
    // log::LogSink::ptr file_sink=log::SinkFactory::create<FileSink>("./logfile/test.log");//指定文件
    // log::LogSink::ptr roll_sink=log::SinkFactory::create<RollBySizeSink>("./logfile/roll-",1024*1024);//滚动文件

    // std_sink->log(str.c_str(),str.size());
    // file_sink->log(str.c_str(),str.size());

    // size_t count=0;
    // size_t cur_size=0;
    // while(cur_size<1024*1024*10)
    // {
    //     std::string out=std::to_string(count++)+str;
    //     roll_sink->log(out.c_str(),out.size());
    //     cur_size+=out.size();
    // }

    // //日志格式化测试
    // log::LogMsg msg(log::Level::value::INFO,53,"main.cc","root","格式化功能测试...");
    // //log::Formatter fmt;
    // log::Formatter fmt("abcd[%d{%H:%M:%S}][%t][%f:%l]%T%m%n");
    // std::string str=fmt.format(msg);
    // std::cout<<str<<std::endl;

    ////util工具类测试
    // std::cout<<"current timestamp:"<<Date::getTime()<<std::endl;
    // std::string pathName="abc/def/a.txt";
    // File::CreateDirectory(File::path(pathName));
}